package com.bw.educate.controller;


import com.alibaba.fastjson.JSON;
import com.bw.educate.common.ResponseResult;
import com.bw.educate.common.tree.TreeCategory;
import com.bw.educate.entity.EduCategoryChild;
import com.bw.educate.service.IEduCategoryChildService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-12
 */
@RestController
@RequestMapping("category")
public class EduCategoryChildController {


    @Autowired
    private IEduCategoryChildService categoryChildService;

    private String filePath="E:\\java\\shixun\\education\\code\\view\\educate-C\\";


    /**
     * @Description:专业树保存
     * @Author: 李闯
     * @Date: 2020/8/18 20:15
     * @param categoryChild:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("save")
    public ResponseResult addSave(@RequestBody EduCategoryChild categoryChild){

        ResponseResult result=ResponseResult.SUCCESS();
        try {
            if(categoryChild.getId()==null){
                categoryChildService.saveOrUpdate(categoryChild);
            }else{
                categoryChildService.updateById(categoryChild);
            }
            result.setMessage("保存成功");
            getHtml(categoryChildService.getTree());
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("保存失败"+e.toString());
        }

        return result;
    }

    /**
     * @Description:获取专业树
     * @Author: 李闯
     * @Date: 2020/8/18 20:15
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("getTree")
    public ResponseResult getTree(){

        ResponseResult result=ResponseResult.SUCCESS();
        try {
            result.setResult(categoryChildService.getTree());
            result.setMessage("保存成功");

        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("保存失败"+e.toString());
        }

        return result;
    }


    /**
     * @Description:根据id获取专业
     * @Author: 李闯
     * @Date: 2020/8/18 20:15
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("get/{id}")
    public ResponseResult get(@PathVariable("id") Long id) {//回显菜单
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EduCategoryChild categoryChild = categoryChildService.getById(id);
            result.setResult(categoryChild);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询失败:" + e.toString());
        }
        return result;
    }

    /**
     * @Description:根据id删除专业树
     * @Author: 李闯
     * @Date: 2020/8/18 20:16
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("delTree/{id}")
    public ResponseResult delTree(@PathVariable("id") Long id) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            //删除的逻辑，两条sql
            categoryChildService.delTree(id);
            result.setMessage("删除成功");
            getHtml(categoryChildService.getTree());
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("删除失败:" + e.toString());
        }
        return result;
    }

    //生成一个课程静态页面
    public void getHtml(List<TreeCategory> treeCategoryList) {

        try {
            Configuration configuration = new Configuration(Configuration.getVersion());//创建模板引擎配置
            ClassPathResource classPathResource = new ClassPathResource("template");
            //加载模板的路径【文件目录或者文件夹】
            configuration.setDirectoryForTemplateLoading(classPathResource.getFile());
            configuration.setDefaultEncoding("utf-8");//设置编码格式
            Template template = configuration.getTemplate("courselist.html");

            Map map = new HashMap();
            map.put("parentlist", JSON.toJSONString(treeCategoryList));
            //模板+数据合成的具体的页面
            String htmlStr = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
            InputStream inputStream = IOUtils.toInputStream(htmlStr, "utf-8");//将字符串转换成输入流
            File file = new File(filePath + "courselist.html");
            if (file.exists()) {
                file.delete();
            }
            FileOutputStream outputStream = new FileOutputStream(filePath + "courselist.html");
            IOUtils.copy(inputStream, outputStream);
            outputStream.close();
            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
